//
//  MeMainViewController.m
//  RainbowFM
//
//  Created by jv on 14-12-6.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "MeMainViewController.h"
#import "MeMainTableViewCell.h"
#import "FeedbackViewController.h"
#import "LeaveWordsViewController.h"

#import "UserModel.h"
#import "LoginModel.h"

@interface MeMainViewController ()

@end

@implementation MeMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self _initVar];
    [self _initView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUserHead) name:@"UserHeadUpdate" object:nil];
}

-(void)updateUserHead{
    [_headerView.headImageView sd_setImageWithURL:[NSURL URLWithString:[UserModel sharedModel].userInfo.headPic]];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
}

#pragma mark - init

- (void)_initVar
{
    
    _selIndex = -1;
    
    _titleData = @[
                   @"个人资料",
                   @"我的订单",
                   @"我的奖品",
                   @"我的意见",
                   @"收货地址",
                   @"基本设置"
                   ];
    
    
    _imagePData = @[
                    [UIImage imageNamed:@"me_myfile_p"],
                    [UIImage imageNamed:@"me_mybill_p"],
                    [UIImage imageNamed:@"me_myprize_p"],
                    [UIImage imageNamed:@"me_myadvice_p"],
                    [UIImage imageNamed:@"me_myaddress_p"],
                    [UIImage imageNamed:@"me_mysetting_p"]
                    ];
    
    _imageNData = @[
                    [UIImage imageNamed:@"me_myfile_n"],
                    [UIImage imageNamed:@"me_mybill_n"],
                    [UIImage imageNamed:@"me_myprize_n"],
                    [UIImage imageNamed:@"me_myadvice_n"],
                    [UIImage imageNamed:@"me_myaddress_n"],
                    [UIImage imageNamed:@"me_mysetting_n"]
                    ];
}

- (void)_initView
{
    _headerView = _LoadXib(MeHeadView, nil, nil);
    _headerView.width = self.view.width;
    _headerView.height = 250.f;
    _headerView.nameLabel.text = [UserModel sharedModel].userInfo.nickName;
    [_headerView.headImageView sd_setImageWithURL:[NSURL URLWithString:[UserModel sharedModel].userInfo.headPic]];
    self.tableView.tableHeaderView = _headerView;
}

#pragma mark - table datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_titleData count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeMainCell"];
    
    BOOL isFouced = _selIndex == indexPath.row;
    
    cell.isFoucesd = isFouced;
    
    if (isFouced) {
        cell.iconImageView.image = _imagePData[indexPath.row];
    }else{
        cell.iconImageView.image = _imageNData[indexPath.row];
    }
    
    cell.infoLabel.text = _titleData[indexPath.row
                                     ];
    
    return cell;
    
}



#pragma mark - table delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 74.f;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selIndex = indexPath.row;
    
    [self.tableView reloadData];
    
    UIViewController *ctrl;
    switch (indexPath.row) {
        case 0://资料
        {
            ctrl = [self.storyboard instantiateViewControllerWithIdentifier:@"MeFile"];
        }
            break;
        case 1://订单
        {
            ctrl = [self.storyboard instantiateViewControllerWithIdentifier:@"MeBill"];
        }
            break;
        case 2://奖品
        {
            ctrl = [self.storyboard instantiateViewControllerWithIdentifier:@"MePrize"];
        }
            break;
        case 3://意见
        {
            if ([LoginModel sharedModel].userID == nil) {
                UINavigationController *controller = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateInitialViewController];
                [self presentViewController:controller animated:YES completion:nil];
                return;
            }
            
            UIStoryboard *board = [UIStoryboard storyboardWithName:@"Comperes" bundle:nil];
            LeaveWordsViewController *leaveWordsViewController = [board instantiateViewControllerWithIdentifier:@"LeaveWordsViewController"];
            [leaveWordsViewController setPart:InterlocutionPartOfMyself];
            [self.navigationController pushViewController:leaveWordsViewController animated:YES];
            
        }
            break;
        case 4://地址
        {
            ctrl = [self.storyboard instantiateViewControllerWithIdentifier:@"MeAddressViewController"];
        }
            break;
        case 5://设置
        {
            ctrl = [self.storyboard instantiateViewControllerWithIdentifier:@"MeSetting"];
            
        }
            break;
        default:
            break;
    }
    
    if (ctrl) {
        [self.navigationController pushViewController:ctrl animated:YES];
    }
    
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
}
- (IBAction)siginOutAction:(id)sender {
    [UserModel sharedModel].userInfo = nil;
    [LoginModel sharedModel].userID = nil;
    [LoginModel sharedModel].token = nil;
    NSUserDefaults *userLogin = [[NSUserDefaults alloc] init];
    [userLogin setObject:nil forKey:@"phone"];
    [userLogin setObject:nil forKey:@"pwd"];
    [userLogin synchronize];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
